<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" >
    <meta http-equiv="X-UA-Compatible" content="IE=11,IE=10,IE=9,IE=8" >
    <meta name="baidu-site-verification" content="dIcXMeY8Ya" />
    
    <title>位运算 | Neseij`s notes</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0" >
    <meta name="keywords" content="java,spring boot,spring cloud,redis,RabbitMQ,kafka,RocketMQ,ES,JVM,IOC,AOP,MySQL,多线程" >
    <meta name="description" content="java,spring boot,spring cloud,redis,RabbitMQ,kafka,RocketMQ,ES,JVM,IOC,AOP,MySQL,多线程" >

    
    <link rel="alternative" href="/atom.xml" title="Neseij`s notes" type="application/atom+xml" >
    
    
    <link rel="shortcut icon" href="/favicon.ico" >
    
    
<link rel="stylesheet" href="/css/style.css">

    <!--[if lt IE 9]>
    
<script src="/js/html5.js"></script>

    <![endif]-->
    
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?d5ebf515ab530cfbdda5f5c85093fb41";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>


<meta name="generator" content="Hexo 5.1.1"></head>

<body class="home">
    <!--[if lt IE 9]>
    <div class="browsehappy">
        当前网页 <strong>不支持</strong>
        你正在使用的浏览器. 为了正常的访问, 请 <a href="http://browsehappy.com/">升级你的浏览器</a>.
    </div>
    <![endif]-->

    <!-- 博客头部 -->
    <header class="header">
    <section class="container header-main">
        <div class="logo">
            <a href="/">
                <div class="cover">
                    <span class="name">Neseij`s notes</span>
                    <span class="description">You can you BiBi</span>
                </div>
            </a>
        </div>
        <div class="dropnav icon-paragraph-justify" id="JELON__btnDropNav"></div>
        <ul class="menu hidden" id="JELON__menu">
            
            <li rel="/2017/06/28/%E4%BD%8D%E8%BF%90%E7%AE%97/index.html" class="item ">
                <a href="/" title="首页" class="icon-home">&nbsp;首页</a>
            </li>
            
            <li rel="/2017/06/28/%E4%BD%8D%E8%BF%90%E7%AE%97/index.html" class="item ">
                <a href="/lab/" title="实验室" class="icon-lab">&nbsp;实验室</a>
            </li>
            
            <li rel="/2017/06/28/%E4%BD%8D%E8%BF%90%E7%AE%97/index.html" class="item ">
                <a href="/about/" title="关于" class="icon-about">&nbsp;关于</a>
            </li>
            
            <li rel="/2017/06/28/%E4%BD%8D%E8%BF%90%E7%AE%97/index.html" class="item ">
                <a href="/comment/" title="留言" class="icon-comment">&nbsp;留言</a>
            </li>
            
        </ul>
        <div class="profile clearfix">
            <div class="feeds fl">
                
                
                <p class="links">
                    
                        <a href="https://github.com/huangjiesen" target="_blank">Github</a>
                        |
                    
                        <a href="https://www.gitee.com/hhjiesen" target="_blank">Gitee</a>
                        
                    
                </p>
                <p class="sns">
                    
                    <a href="javascript: void(0);" class="wechat">
                        <b>■</b>
                        公众号
                        <span class="popover">
                            <img src="/img/wechat_mp.jpg" width="120" height="120" alt="我的微信订阅号">
                            <i class="arrow"></i>
                        </span>
                    </a>
                </p>
                
            </div>
            <div class="avatar fr">
                <img src="/img/sen.jpg" alt="avatar" title="Jelon" >
            </div>
        </div>
    </section>
</header>


    <!-- 博客正文 -->
    <div class="container body clearfix">
        <section class="content">
            <div class="content-main widget">
                <!-- 文章页 -->
<!-- 文章 -->
<article class="post article">
    <header class="text-center">
        <h3 class="post-title"><span>位运算</span></h3>
    </header>
    <p class="post-meta text-center">
        Jelon 发表于
        <time datetime="2017-06-28T06:11:04.000Z">2017-06-28</time>
    </p>
    <div class="post-content">
        <h1 id="6种位运算"><a href="#6种位运算" class="headerlink" title="6种位运算"></a>6种位运算</h1><p><code>&amp;</code> 位与运算 、 <code>|</code> 位或运算 、 <code>~</code> 位反运算 、 <code>^</code> 异或运算 、 <code>&gt;&gt;</code> 右移位运算 、 <code>&lt;&lt;</code> 左移位运算</p>
<a id="more"></a>
<h2 id="1-位与运算-amp"><a href="#1-位与运算-amp" class="headerlink" title="1. 位与运算(&amp;)"></a>1. 位与运算(<code>&amp;</code>)</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdint.h&gt;</span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * &amp; 位与运算</span></span><br><span class="line"><span class="comment">     * 对比两个二进制数据的位，如果对应的位数均为1则结果为1，否则为0</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">int</span> a = <span class="number">0b1001</span>;</span><br><span class="line">    <span class="keyword">int</span> b = <span class="number">0b1010</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>,a &amp; b);  <span class="comment">//位与 运算后的二进制结果为 0b1000  所以输出的十进制结果为8</span></span><br><span class="line">    <span class="keyword">int8_t</span> c = <span class="number">20</span>;  <span class="comment">//对应8位的二进制为 0001 0100</span></span><br><span class="line">    <span class="keyword">int8_t</span> d = <span class="number">30</span>;  <span class="comment">//对应8位的二进制为 0001 1110</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>,d &amp; c);  <span class="comment">//位与 运算后的二进制结果为 0001 0100  所以输出的十进制结果为20</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="2-位或运算"><a href="#2-位或运算" class="headerlink" title="2. 位或运算(|)"></a>2. 位或运算(<code>|</code>)</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdint.h&gt;</span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * | 位或运算</span></span><br><span class="line"><span class="comment">     * 对比两个二进制数据的位，如果对应的位数均为0则结果为0，否则为1</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">int</span> a = <span class="number">0b1001</span>;</span><br><span class="line">    <span class="keyword">int</span> b = <span class="number">0b1010</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>,a | b);  <span class="comment">//位或 运算后的二进制结果为 0b1011  所以输出的十进制结果为11</span></span><br><span class="line">    <span class="keyword">int8_t</span> c = <span class="number">20</span>;  <span class="comment">//对应8位的二进制为 0001 0100</span></span><br><span class="line">    <span class="keyword">int8_t</span> d = <span class="number">30</span>;  <span class="comment">//对应8位的二进制为 0001 1110</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>,d | c);  <span class="comment">//位或 运算后的二进制结果为 0001 1110  所以输出的十进制结果为30</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="3-位反运算"><a href="#3-位反运算" class="headerlink" title="3. 位反运算(~)"></a>3. 位反运算(<code>~</code>)</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdint.h&gt;</span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * ~ 位反运算</span></span><br><span class="line"><span class="comment">     * 对二进制数据进行取反，1变成0、0变成1</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * 假设有一 int 类型的数，值为5，那么，我们知道它在计算机中表示为：</span></span><br><span class="line"><span class="comment">     * 00000000 00000000 00000000 00000101</span></span><br><span class="line"><span class="comment">     * 5转换成二制是101，不过int类型的数占用4字节（32位），所以前面填了一堆0。</span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * 二进制正数与负数的表示：前面的一位表示符号位、正数为0，负数为1</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * 现在想知道，-5在计算机中如何表示？</span></span><br><span class="line"><span class="comment">     * 在计算机中，负数以其正值的补码形式表达。</span></span><br><span class="line"><span class="comment">     * 什么叫补码呢？这得从原码，反码说起。</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * 原码：一个整数，按照绝对值大小转换成的二进制数，称为原码。</span></span><br><span class="line"><span class="comment">     * 比如 00000000 00000000 00000000 00000101 是 5的 原码。</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * 反码：将二进制数按位取反，所得的新二进制数称为原二进制数的反码。</span></span><br><span class="line"><span class="comment">     * 取反操作指：原为1，得0；原为0，得1。（1变0; 0变1）</span></span><br><span class="line"><span class="comment">     * 比如：将00000000 00000000 00000000 00000101每一位取反，得11111111 11111111 11111111 11111010。</span></span><br><span class="line"><span class="comment">     * 称：11111111 11111111 11111111 11111010 是 00000000 00000000 00000000 00000101 的反码,反码是相互的</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * 补码：反码加1称为补码。</span></span><br><span class="line"><span class="comment">     * 也就是说，要得到一个数的补码，先得到反码，然后将反码加上1，所得数称为补码。</span></span><br><span class="line"><span class="comment">     * 比如：00000000 00000000 00000000 00000101 的反码是：11111111 11111111 11111111 11111010。</span></span><br><span class="line"><span class="comment">     * 那么，补码为：11111111 11111111 11111111 11111010 + 1 = 11111111 11111111 11111111 11111011</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">int8_t</span> a = <span class="number">3</span>; <span class="comment">//3对应8位的二进制数据为 0000 0011</span></span><br><span class="line">    a = ~a;       <span class="comment">//3位返 运算后的结果为   1111 1101</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>,a);  <span class="comment">//输出结果为-4</span></span><br><span class="line">    <span class="keyword">uint8_t</span> b = <span class="number">5</span>;     <span class="comment">//无符号的最大储值是255</span></span><br><span class="line">    b = ~b;            <span class="comment">//无符号的5位返后的值为 255 - 5 = 250</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>,b);  <span class="comment">//结果为250 无符号的int位反后的值为最大储值减要位反的值</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="4-异或运算"><a href="#4-异或运算" class="headerlink" title="4. 异或运算(^)"></a>4. 异或运算(<code>^</code>)</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdint.h&gt;</span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * ^ 异或</span></span><br><span class="line"><span class="comment">     * 对比两个二进制数据的位，如果对应的位数不相同则为1，否则为0</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">int8_t</span> a=<span class="number">0b0001</span>;</span><br><span class="line">    <span class="keyword">int8_t</span> b=<span class="number">0b0011</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>, a^b);  <span class="comment">//异或运算后的结果为0b0010、输出结果为2</span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="5-右移位运算-gt-gt"><a href="#5-右移位运算-gt-gt" class="headerlink" title="5. 右移位运算(&gt;&gt;)"></a>5. 右移位运算(<code>&gt;&gt;</code>)</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">    * &gt;&gt; 右移</span></span><br><span class="line"><span class="comment">    * 示例：</span></span><br><span class="line"><span class="comment">    *      2 &gt;&gt; 1 = 1   // 0b0010 左移一位后的结果为 0b0001</span></span><br><span class="line"><span class="comment">    * 和左移位相反将二进制数向右移位</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>, <span class="number">5</span>&gt;&gt;<span class="number">1</span>);  <span class="comment">//0b0101移一位为0b0010 结果为2</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>, <span class="number">5</span>&gt;&gt;<span class="number">2</span>);  <span class="comment">//0b0101移二位为0b0001 结果为1</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>, <span class="number">5</span>&gt;&gt;<span class="number">3</span>);  <span class="comment">//0b0101移三位为0b0000 结果为0</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>, <span class="number">5</span>&gt;&gt;<span class="number">4</span>);  <span class="comment">//0b0101移四位为0b0000 结果为0</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>, <span class="number">6</span>&gt;&gt;<span class="number">1</span>);  <span class="comment">//0b0110移一位为0b0011 结果为3</span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="6-左移位运算-lt-lt"><a href="#6-左移位运算-lt-lt" class="headerlink" title="6. 左移位运算(&lt;&lt;)"></a>6. 左移位运算(<code>&lt;&lt;</code>)</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">    * &lt;&lt; 左移</span></span><br><span class="line"><span class="comment">    * 示例：</span></span><br><span class="line"><span class="comment">    *      2 &lt;&lt; 1 = 4   // 0b0010 左移一位后的结果为 0b0100</span></span><br><span class="line"><span class="comment">    * 将二进制数向左移位、移位后右边用0填充</span></span><br><span class="line"><span class="comment">    * 向左移一位后的值，刚好为原值乘2。</span></span><br><span class="line"><span class="comment">    * 用这种移位的方式作乘法运算是最快的</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>, <span class="number">5</span>&lt;&lt;<span class="number">1</span>);  <span class="comment">//0b0101移一位为0b1010 结果为10 类似 5*2</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>, <span class="number">5</span>&lt;&lt;<span class="number">2</span>);  <span class="comment">//0b0101移二位为0b10100 结果为20 类似 5*2*2</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>, <span class="number">5</span>&lt;&lt;<span class="number">3</span>);  <span class="comment">//0b0101移三位为0b101000 结果为40 类似 5*2*2*2</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>, <span class="number">5</span>&lt;&lt;<span class="number">4</span>);  <span class="comment">//0b0101移四位为0b1010000 结果为80 类似 5*2*2*2*2</span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="7-示例-使用位运算提取颜色通道值"><a href="#7-示例-使用位运算提取颜色通道值" class="headerlink" title="7. 示例-使用位运算提取颜色通道值"></a>7. 示例-使用位运算提取颜色通道值</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdint.h&gt;</span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 通常用十六进制保存颜色值</span></span><br><span class="line"><span class="comment">     * 0xFFEEDDCC 每两位一个通道，四个通道分别表示 ARGB(Alpha,Red,Greed,Blue)</span></span><br><span class="line"><span class="comment">     * 可以通过红绿蓝及透明度混合成任何一种颜色</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">uint32_t</span> color = <span class="number">0xFFEEDDCC</span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 现在要通要取红色通道的值</span></span><br><span class="line"><span class="comment">     * 首先用位与运算去掉其它通道的值</span></span><br><span class="line"><span class="comment">     * 0b11111111 11101110 11011101 11001100   //color值</span></span><br><span class="line"><span class="comment">     * 0b00000000 11101110 00000000 00000000   //位与0x00FF0000后的值</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">uint32_t</span> temp = color &amp; <span class="number">0x00FF0000</span>;</span><br><span class="line">    <span class="comment">//再将0b00000000 11101110 00000000 00000000右移16位所得的即为红色通道的值</span></span><br><span class="line">    <span class="keyword">uint8_t</span> red=temp&gt;&gt;<span class="number">16</span>;  <span class="comment">//红色通道EE,结果输出238</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>, red);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
    </div>
    <p class="post-meta">
        <span class="post-cat">分类：
            <a class="cat-link" href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">数据结构</a>
        </span>
        <span class="post-tags">
            标签：
            
    
        <a href="/tags/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" title="数据结构">数据结构</a>
    

        </span>
    </p>
</article>
<!-- 分享按钮 -->

  <div class="article-share clearfix text-center">
    <div class="share-area">
      <span class="share-txt">分享到：</span>
      <a href="javascript: window.open('http://service.weibo.com/share/share.php?url=' + encodeURIComponent(location.href) + '&title=' + document.title + '&language=zh_cn');" class="share-icon weibo"></a>
      <a href="javascript: alert('请复制链接到微信并发送');" class="share-icon wechat"></a>
      <a href="javascript: window.open('http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=' + encodeURIComponent(location.href) + '&title=' + document.title);" class="share-icon qqzone"></a>
      <a href="javascript: window.open('http://connect.qq.com/widget/shareqq/index.html?url=' + encodeURIComponent(location.href) + '&desc=Jelon个人博客&title=' + document.title + '&callback=' + encodeURIComponent(location.href));" class="share-icon qq"></a>
      <a href="javascript: window.open('http://shuo.douban.com/!service/share?href=' + encodeURIComponent(location.href) + '&name=' + document.title + '&text=' + document.title);" class="share-icon douban"></a>
    </div>
  </div>


<!-- 上一篇/下一篇 -->

<!-- 文章评论 -->

  
<script src="/js/comment.js"></script>

  <div id="comments" class="comment">
    <!--
    <div class="sign-bar">
      GitHub 已登录!
      <span class="sign-link">登出</span>
    </div>
    <section class="box">
      <div class="com-avatar"><img src="/img/jelon.jpg" alt="avatar"></div>
      <div class="com-text">
        <div class="main">
          <textarea class="text-area-edited show" placeholder="欢迎评论！"></textarea>
          <div class="text-area-preview"></div>
        </div>
        <div class="switch">
          <div class="switch-item on">编辑</div>
          <div class="switch-item">预览</div>
        </div>
        <div class="button">提交</div>
      </div>
    </section>
    <section class="tips">注：评论支持 markdown 语法！</section>
    <section class="list-wrap">
      <ul class="list">
        <li>
          <div class="user-avatar">
            <a href="/">
              <img src="/img/jelon.jpg" alt="user-avatar">
            </a>
          </div>
          <div class="user-comment">
            <div class="user-comment-header">
              <span class="post-name">张德龙</span>
              <span class="post-time">2017年12月12日</span>
              <span class="like liked">已赞</span>
              <span class="like-num">2</span>
            </div>
            <div class="user-comment-body">333333</div>
          </div>
        </li>
        <li>
          <div class="user-avatar">
            <a href="/">
              <img src="/img/jelon.jpg" alt="user-avatar">
            </a>
          </div>
          <div class="user-comment">
            <div class="user-comment-header">
              <span class="post-name">刘德华</span>
              <span class="post-time">2017年12月12日</span>
              <span class="like">点赞</span>
              <span class="like-num">2</span>
            </div>
            <div class="user-comment-body">vvvvv</div>
          </div>
        </li>
      </ul>
      <div class="page-nav">
        <a href="javascript: void(0);" class="item">1</a>
        <a href="javascript: void(0);" class="item">2</a>
        <a href="javascript: void(0);" class="item current">3</a>
      </div>
    </section>
    -->
  </div>
  <script>
  JELON.Comment({
    container: 'comments',
    label: '位运算' || '2017/06/28/位运算/',
    owner: 'huangjiesen',
    repo: 'blog_comments',
    clientId: '041be489cd61a2287ad9',
    clientSecret: 'bae2b06e9d47f18c2b3dbb85636c5aefd4ee4355'
  });
  </script>


            </div>

        </section>
        <!-- 侧栏部分 -->
<aside class="sidebar">
    
    <section class="widget">
        <h3 class="widget-hd"><strong>文章搜索</strong></h3>
        <div class="search-form">
  <form
    id="searchForm"
    method="GET"
    action="https://www.baidu.com/s"
    ectype="application/x-www-form-urlencoded"
    target="_blank"
    autocomplete="false"
    onsubmit="javascript: return false;">
    <input
      id="searchKeyword"
      type="text"
      class="form-control"
      placeholder="输入关键字搜索"
      autocomplete="false"
    />
    <input id="searchKeywordHidden" type="hidden" name="wd" />
    <input id="searchButton" class="btn" type="submit" value="搜索" />
  </form>
</div>
    </section>
    

    <section class="widget">
        <h3 class="widget-hd"><strong>文章分类</strong></h3>
        <!-- 文章分类 -->
<ul class="widget-bd">
    
    <li>
        <a href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">数据结构</a>
        <span class="badge">(1)</span>
    </li>
    
</ul>
    </section>

    
    <section class="widget">
        <h3 class="widget-hd"><strong>热门标签</strong></h3>
        <!-- 文章标签 -->
<div class="widget-bd tag-wrap">
  
    <a class="tag-item" href="/tags/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" title="数据结构">数据结构 (1)</a>
  
</div>
    </section>
    

    

    
</aside>
<!-- / 侧栏部分 -->
    </div>

    <!-- 博客底部 -->
    <footer class="footer">
    &copy;
    
        2016-2020
    

    <a href="/">Jelon Loves You</a>
</footer>
<div class="back-to-top" id="JELON__backToTop" title="返回顶部">返回顶部</div>

    <!--博客js脚本 -->
    <!-- 这里放网站js脚本 -->

<script src="/js/main.js?v=1599545853011.js"></script>

</body>
</html>